package com.sicent.upms.operate.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sicent.operate.common.base.dao.PageInfo;
import com.sicent.operate.common.result.ApiResult;
import com.sicent.upms.operate.data.UserData;
import com.sicent.upms.operate.model.ManagerResource;
import com.sicent.upms.operate.model.ManagerRole;
import com.sicent.upms.operate.model.ManagerUserInfo;

@RestController
@RequestMapping("admin/user")
public class ManagerUserController {

	@Autowired
	private UserData userData;
	
	@RequestMapping("/getPageInfo")
	public ApiResult getPageInfo(Integer pageNo,Integer pageSize){
		PageInfo<ManagerUserInfo> pageInfo=new PageInfo<ManagerUserInfo>(pageNo, pageSize);
		return new ApiResult(userData.findByPage(pageInfo));
	}
	
	@RequestMapping("/addUser")
	public ApiResult addUser(ManagerUserInfo managerUserInfo){
		try {
			if(StringUtils.isBlank(managerUserInfo.getUserName())||StringUtils.isBlank(managerUserInfo.getUserPassword())){
				throw new RuntimeException("用户名密码不能为空");
			}
			userData.addUser(managerUserInfo);
			return new ApiResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult(e.getMessage());
		}
	}
	
	@RequestMapping("/getAllResource")
	public ApiResult getAllResource(){
		return new ApiResult(userData.getAllResource());
	}
	
	@RequestMapping(value="/addResource",method=RequestMethod.POST)
	public ApiResult addResource(ManagerResource resource){
		try {
			userData.addResource(resource);
			return new ApiResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult(e.getMessage());
		}
	}
	
	@RequestMapping(value="/getAllRoles",method=RequestMethod.GET)
	public ApiResult getAllRoles(Integer pageNo,Integer pageSize){
		PageInfo<ManagerRole> page=new PageInfo<ManagerRole>(pageNo, pageSize);
		return new ApiResult(userData.getAllRole(page));
	}
	
	@RequestMapping(value="/addRole",method=RequestMethod.POST)
	public ApiResult addRole(ManagerRole role){
	    try {
	    	userData.addRole(role);
	    	return new ApiResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult("添加出错啦");
		}
	}
	
	@RequestMapping(value="/addRoleResource",method=RequestMethod.POST)
	public ApiResult addRoleResource(@RequestParam(required=true)Integer roleId,
			@RequestParam(required=true) String roles){
		try {
			userData.addRoleResource(roleId, roles);
			return new ApiResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult(e.getMessage());
		}
	}
	
	@RequestMapping("/getAllUseRole")
	public ApiResult getAllUseRole(){
		try {
			userData.getAllUseRole();
			return new ApiResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult(e.getMessage());
		}
	}
	
}
